'use strict';
const fs = require('fs')
const inquirer = require('inquirer');
const Command = require('@hl-imooc-cli-dev/command');
const log = require("@hl-imooc-cli-dev/log");
// const { type } = require("os");

class InitCommand extends Command {
    init(){
        this.projectName = this._argv[0] || '';
        // console.log('this._cmd=====', this._cmd.opts)
        // this.force = !!this._cmd.opts().force
        log.verbose('projectName',this.projectName)
        // log.verbose('force',this.force)
    }
    async exec(){
        try{
            // 1，准备阶段
            await this.prepare()
            // 2，下载模版
            // 3，安装模版

        } catch(e){
        }
        console.log('init 的业务逻辑')
    }
    async prepare (){
        // 1，判断当前目录是否为空
        if(!this.isCwdEmpty()){
            // 1.1 询问是否继续创建
            const {ifContinue } = await inquirer
            .prompt([
                {
                    type:"confirm",
                    name:"ifContinue",
                    message:"当前文件夹不为空，是否继续创建项目",
                    default: false
                }
            ])
            if(ifContinue) {
                // 清空当前目录
                console.log('ifContinue===', ifContinue)
            }
        }
        // 2，是否启动强制更新
        // 3，选择创建的项目或文件
        // 4，获取项目的基本信息

    }
    isCwdEmpty(){
        const loacalPath = process.cwd()
        let fileList = fs.readdirSync(loacalPath)
        // 文件过滤逻辑
        fileList = fileList.filter(file => {
           return !file.startsWith('.') && ['node_modules'].indexOf(file) < 0
        });
        return !fileList || fileList.length <=0
        // console.log('filelist', fileList)
    }

}
function init(argv) {
    // TODO
    // console.log('init command 已执行')
    // console.log(projectName, cmdObj.force)
    return new InitCommand(argv)
}

module.exports = init
module.exports.InitCommand = InitCommand;